package com.dci.service.impl.management;

import com.alibaba.fastjson.JSONObject;
import com.dci.dao.pms.management.ProjectManageInfoMapper;
import com.dci.dao.pms.management.TaskInfoMapper;
import com.dci.model.management.ProjectManageInfo;
import com.dci.model.management.TaskInfo;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.service.management.ProjectManageService;
import com.dci.util.UUIDBuild;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.management
 * @Description: 项目管理库实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectManageServiceImpl implements ProjectManageService {


    @Resource
    private ProjectManageInfoMapper projectManageInfoMapper;

    @Resource
    private TaskInfoMapper taskInfoMapper;


    /**
     * 保存
     *
     * @param record
     * @return
     */
    @Override
    public JsonData saveProjectManageInfo(ProjectManageInfo record) {
        record.setId(UUIDBuild.getUUID());
        record.setStatus(0);
        record.setAuditStatus(0);
        // 获取当前系统时间和日期并格式化输出:
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 格式转换.String
        String dateTime = df.format(date);
        record.setDeclareTime(dateTime);
        projectManageInfoMapper.insert(record);
        record = projectManageInfoMapper.selectByPrimaryKey(record);

        // 新增任务管理
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setId(UUIDBuild.getUUID());
        taskInfo.setProjectId(record.getId());
        taskInfo.setType(record.getType());
        taskInfo.setName(record.getName());
        // 默认状态为0
        taskInfo.setStatus(0);
        taskInfo.setHead(record.getHead());
        taskInfoMapper.insert(taskInfo);
        return JsonData.success(record, 906);
    }

    /**
     * 查询
     *
     * @param record
     * @return
     */
    @Override
    public JsonData getProjectManageInfo(ProjectManageInfo record) {
        record = projectManageInfoMapper.selectByPrimaryKey(record);
        return JsonData.success(record, 906);
    }


    /**
     * 古保资金项目更新
     *
     * @param record
     * @return
     */
    @Override
    public JsonData batchUpdateManageInfo(ProjectManageInfo record) {
        record.setAuditStatus(1);
        projectManageInfoMapper.batchUpdateManageInfo(record);
        return JsonData.success(record, 906);
    }


    /**
     * 非古保资金项目更新
     *
     * @param record
     * @return
     */
    @Override
    public JsonData batchUpdateManageInfoFei(ProjectManageInfo record) {
        record.setAuditStatus(1);
        projectManageInfoMapper.batchUpdateManageInfo(record);
        return JsonData.success(record, 906);
    }

    @Override
    public JsonData updateBySub(ProjectManageInfo record) {
        record.setAuditStatus(1);
        projectManageInfoMapper.updateBySub(record);
        return JsonData.success(record, 906);
    }

    /**
     * 查询列表数据
     *
     * @param record
     * @return
     */
    @Override
    public PageResult selectProjectManageList(ProjectManageInfo record) {
        // 分页插件
        PageHelper.startPage(record.getPageNum(), record.getPageSize());
        List< ProjectManageInfo > list = projectManageInfoMapper.selectProjectManageList(record);
        PageInfo< ProjectManageInfo > pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }

    @Override
    public PageResult selectAll(String form) {

        ProjectManageInfo pro = new ProjectManageInfo();
        pro.setName(form);

        List< ProjectManageInfo > list = projectManageInfoMapper.selectAll(pro);
        for (ProjectManageInfo li : list) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = li.getCreateTime();
            li.setFound_date(sdf.format(date));
        }
        PageInfo< ProjectManageInfo > pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }

    @Override
    public PageResult selectAllEnd(String form) {

        ProjectManageInfo pro = new ProjectManageInfo();
        pro.setName(form);

        List< ProjectManageInfo > list = projectManageInfoMapper.selectAllEnd(pro);
        for (ProjectManageInfo li : list) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = li.getCreateTime();
            li.setFound_date(sdf.format(date));
        }
        PageInfo< ProjectManageInfo > pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }

    @Override
    public JsonData saveOtherField(String fields) {
        String[] field = new String[0];
        JSONObject jsonObject = new JSONObject();
        if (fields != null) {
            field = fields.split(",");
        }
        String sql = "ALTER TABLE pms_project_manager ";
        String addSql = "";
        for (int i = 0; i < field.length; i++) {
            String column = "column_" + i;
            jsonObject.put(field[i], column);
            if (addSql == "") {
                addSql += " ADD " + column + "  VARCHAR(500) NULL ";
            } else {
                addSql += " ,ADD " + column + "  VARCHAR(500) NULL ";
            }
        }

        sql += addSql;
        int ret = projectManageInfoMapper.saveOtherField(sql);
        projectManageInfoMapper.updateFieldName(jsonObject.toString());
        String defuat = "alter table pms_project_manager alter field_name drop default";
        projectManageInfoMapper.updateDefuat(defuat);
        String defuatSql = "alter table pms_project_manager alter field_name set default '" + jsonObject.toString() + "'";
        projectManageInfoMapper.updateDefuatFieldName(defuatSql);
        return JsonData.success(null, 906);
    }

    @Override
    public JsonData updateOtherField(String oldFields, String newFields) {
        String[] newNames = new String[0];
        String[] oldNames = new String[0];
        int ret = 0;
        if (null != newFields && newFields != "") {
            newNames = newFields.split(",");
        }
        if (null != oldFields && oldFields != "") {
            oldNames = oldFields.split(",");
        }
        if (oldNames.length > 0) {
            for (int i = 0; i < oldNames.length; i++) {
                String delSql = "alter table pms_project_manager drop column_" + i;
                ret = projectManageInfoMapper.delOtherField(delSql);
            }
            projectManageInfoMapper.updateFieldName("");
            String defuat = "alter table pms_project_manager alter field_name drop default";
            projectManageInfoMapper.updateDefuat(defuat);
        }
        if (newNames.length > 0) {
            JSONObject jsonObject = new JSONObject();
            String sql = "ALTER TABLE pms_project_manager ";
            String addSql = "";
            for (int i = 0; i < newNames.length; i++) {
                String column = "column_" + i;
                jsonObject.put(newNames[i], column);
                if (addSql == "") {
                    addSql += " ADD " + column + "  VARCHAR(500) NULL ";
                } else {
                    addSql += " ,ADD " + column + "  VARCHAR(500) NULL ";
                }
            }

            sql += addSql;
            int result = projectManageInfoMapper.saveOtherField(sql);
            projectManageInfoMapper.updateFieldName(jsonObject.toString());
            String defuat = "alter table pms_project_manager alter field_name drop default";
            projectManageInfoMapper.updateDefuat(defuat);
            String defuatSql = "alter table pms_project_manager alter field_name set default '" + jsonObject.toString() + "'";
            projectManageInfoMapper.updateDefuatFieldName(defuatSql);
        }
        return JsonData.success(null, 906);
    }

    @Override
    public JsonData queryOtherField() {
        String colums = projectManageInfoMapper.queryOtherField();
        return JsonData.success(colums, 906);
    }

}
